package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.entity.UcenterMemberVo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 董万均
 * @since 2020-11-03
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录，这需要验证用户是否登录,使用的是单点登录中的token验证
    @PostMapping("login") //使用RequestBody需要post有提交，如果是get提交就会出错
    public Result loginUser(@RequestBody UcenterMember member){
        //返回token值，使用jwt生成
        String token=memberService.loginConfirmation(member);
        return Result.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public Result registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return Result.ok();
    }

    //根据token获取用户的登录信息
    @GetMapping("getLoginInfo")
    public Result getLoginInfo(HttpServletRequest request){
        //调用jwt中的方法，根据request对象获取头信息，返回登录id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return Result.ok().data("loginInfo",member);
    }


    //获取订单中用户的信息
    @PostMapping("getUserInfo/{id}")
    public UcenterMemberVo getUserInfo(@PathVariable String id){
        UcenterMember ucenterMember = memberService.getById(id);//id是用户id
        UcenterMemberVo memberVo=new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember,memberVo);
        return memberVo;
    }

    //统计某一天中的注册的用户
    @GetMapping("registerCount/{day}")
    public Result registerCount(@PathVariable String day){
        Integer count=memberService.registerCount(day);
        return Result.ok().data("registerCount",count);
    }
}

